package com.zcy.model;

import javax.swing.*;
import java.util.*;
import java.util.Map;
import java.util.stream.Collectors;

public class ElmBox {
    //定义集合存储牌
    public static List<Brand> slot = new ArrayList<>();
    //添加卡牌
    public static void add(Brand brand){
        slot.add(brand);
        //将元素进行排序
        slot.sort(Comparator.comparing(Brand::getName));
        Map<String,List<Brand>> map = slot.stream().collect(Collectors.groupingBy(Brand::getName));
        Set<String> set = map.keySet();
        for (String s : set){
            List<Brand> brandList = map.get(s);
            if (brandList.size() == 3){
                deleteByBrandName(s);
                break;
            }
        }
        paint();
        over(brand);
    }

    public static void deleteByBrandName(String name){
        Iterator<Brand> iterator = slot.iterator();
        while (iterator.hasNext()){
            Brand b = iterator.next();
            if (name.equals(b.getName())){
                b.getParent().remove(b);
                iterator.remove();
            }
        }
    }
    public static void paint(){
        for (int i = 0;i < slot.size();i++){
            Brand brand = slot.get(i);
            int x = i * brand.getWidth() + 10;
            brand.setBounds(x,600,50,50);
        }
    }
    public static void over(Brand brand){
        if(slot.size() >= 7){
            JOptionPane.showConfirmDialog(brand,"程序结束");
            System.exit(0);
        }
    }
}
